﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AscendEast_DataEntry
{
    public partial class Form_Centre_Edit : Form
    {
        DataAccess dal = new DataAccess();
        public Form_Centre_Edit()
        {
            InitializeComponent();
            PopulateDropDown_centres();
            
            tb_centre_code_edit_update.Enabled = false;
            tb_centre_name_edit_update.Enabled = false;
            cb_states_centre_edit.Enabled = false;
        }

        private void PopulateDropDown_States_edit_centres()
        {
            string select_states_query = "select * from tbl_states";
            DataTable dt0 = dal.Select(select_states_query);
            DataTable dt = new DataTable();
            dt.Columns.Add("state_code", typeof(String));
            dt.Columns.Add("state_name", typeof(String));
            dt.Rows.Add("null", "Select State");
            for (int i = 0; i < dt0.Rows.Count; i++)
            {
                dt.Rows.Add(dt0.Rows[i][0], dt0.Rows[i][1]);
            }

            cb_states_centre_edit.DisplayMember = dt.Columns["state_name"].ToString();
            cb_states_centre_edit.ValueMember = dt.Columns["state_code"].ToString();
            cb_states_centre_edit.DataSource = dt;
        }

        private void PopulateDropDown_centres()
        {
            string select_centres_query = "select * from tbl_centres";
            DataTable dt0 = dal.Select(select_centres_query);
            DataTable dt = new DataTable();
            dt.Columns.Add("centre_code", typeof(String));
            dt.Columns.Add("centre_name", typeof(String));
            dt.Columns.Add("state_code", typeof(String));
            dt.Rows.Add("null", "Select Centre");
            for (int i = 0; i < dt0.Rows.Count; i++)
            {
                dt.Rows.Add(dt0.Rows[i][0], dt0.Rows[i][1],dt0.Rows[i][2]);
            }

            cb_centre_name_edit_search.DisplayMember = dt.Columns["centre_name"].ToString();
            cb_centre_name_edit_search.ValueMember = dt.Columns["centre_code"].ToString();
            cb_centre_name_edit_search.DataSource = dt;
        }

        private void btn_search_edit_centre_Click(object sender, EventArgs e)
        {
            if (cb_centre_name_edit_search.SelectedIndex == 0)
                MessageBox.Show("Please select any Centre");
            else
            {
                string centre_code = cb_centre_name_edit_search.SelectedValue.ToString();
                PopulateDropDown_States_edit_centres();
                PopulateDataGridView_Centres(centre_code);
                
                tb_centre_name_edit_update.Enabled = true;
                cb_states_centre_edit.Enabled = true;
            }
        }

        private void PopulateDataGridView_Centres(string centre_code)
        {
            string edit_centre_query = "select * from tbl_centres where centre_code = '" + centre_code + "'";
            dgv_centres_edit.DataSource = null;
            dgv_centres_edit.Rows.Clear();
            dgv_centres_edit.Refresh();
            DataTable dt = new DataTable();
            dt = dal.Select(edit_centre_query);

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dgv_centres_edit.Rows.Add(dt.Rows[i][0],dt.Rows[i][1],dt.Rows[i][2]);
            }

            tb_centre_code_edit_update.Text = dt.Rows[0][0].ToString();
            tb_centre_name_edit_update.Text = dt.Rows[0][1].ToString();
            cb_states_centre_edit.SelectedValue = dt.Rows[0][2].ToString();
        }

        private void btn_update_centre_Click(object sender, EventArgs e)
        {
            if (cb_centre_name_edit_search.SelectedIndex == 0)
                MessageBox.Show("Please select any Centre");
            else if (tb_centre_code_edit_update.Text == "")
                MessageBox.Show("Please select any Centre");
            else
            {

                string centre_name_updated = tb_centre_name_edit_update.Text;
                string check_duplicate_query = "select * from tbl_centres where centre_name = '" + centre_name_updated + "'";
                bool duplicate_flag = dal.Check_Duplicate(check_duplicate_query);
                if (duplicate_flag == true)
                    MessageBox.Show("Centre already exists. Please enter another Centre");
                else
                {
                    string centre_code = tb_centre_code_edit_update.Text;
                    string state_code = cb_states_centre_edit.SelectedValue.ToString();
                    string update_state_query = "update tbl_centres set centre_name = '" + centre_name_updated + "', state_code = '" + state_code + "' where centre_code = '" + centre_code + "'";
                    bool updated_flag = dal.Update(update_state_query);
                    if (updated_flag == true)
                        MessageBox.Show("Record Updated Successfully");
                    else
                        MessageBox.Show("Try again");

                    PopulateDataGridView_Centres(centre_code);
                    PopulateDropDown_centres();
                    state_code = null;
                    centre_name_updated = null;
                    tb_centre_code_edit_update.Text = null;
                    tb_centre_name_edit_update.Text = null;
                    cb_states_centre_edit.SelectedIndex = 0;

                    tb_centre_code_edit_update.Enabled = false;
                    tb_centre_name_edit_update.Enabled = false;
                    cb_states_centre_edit.Enabled = false;
                }
            }
        }

        private void btn_cancel_centre_edit_Click(object sender, EventArgs e)
        {
            dgv_centres_edit.DataSource = null;
            dgv_centres_edit.Rows.Clear();
            dgv_centres_edit.Refresh();

            tb_centre_code_edit_update.Text = null;
            tb_centre_name_edit_update.Text = null;
            cb_centre_name_edit_search.SelectedIndex = 0;
            cb_states_centre_edit.SelectedIndex = 0;

            tb_centre_code_edit_update.Enabled = false;
            tb_centre_name_edit_update.Enabled = false;
            cb_states_centre_edit.Enabled = false;
        }

        
    }
}
